<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="手机号">
              <a-input-number style="width:100%" placeholder="请输入手机号" v-model="queryParam.mobile" ></a-input-number>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="用户姓名">
              <a-input placeholder="请输入用户姓名" v-model="queryParam.name" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="性别">
              <a-select default-value="1" placeholder="全部性别" v-model="queryParam.sex" >
                <a-select-option :value="item.val" v-for="(item, index) in sexUl" :key="index" >{{ item.name }}</a-select-option >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="类型">
              <a-select default-value="1" placeholder="全部类型" v-model="queryParam.type" >
                <a-select-option :value="item.val" v-for="(item, index) in typeUl" :key="index" >{{ item.name }}</a-select-option >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="状态">
              <a-select default-value="1" placeholder="全部状态" v-model="queryParam.status" >
                <a-select-option :value="item.val" v-for="(item, index) in statusUl" :key="index" >{{ item.name }}</a-select-option >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="注册时间">
              <a-range-picker @change="panelChange" v-model="queryParamRangePicker"/>
            </a-form-item>
          </a-col>
          <a-col :xl="24" :lg="24" :md="24" :sm="24">
            <div class="tag_box">
              <div class="tag_box_flex">
                <div class="tag_box_flex_title">
                  <span class="tag_box_title">用户标签：</span>
                </div>
                <div class="tag_box_flex_li" :class="!tagShow ? 'tag_box_flex_show':''">
                  <span class="tag_box_flex_btn" id="tag_box_flex_btn">
                    <a-button 
                    class="tag_button"
                    v-for="(item , index) in tagUl" 
                    size="small"
                    :key="index" 
                    :type="item.choose ? 'primary' : '' " 
                    @click="handleTagsChange(index)"> {{item.name}} </a-button>
                    <a-button size="small" id="tag_show_btn1" class="tag_show_btn1" type="primary" @click="funTagShow" v-show="!tagShow">展开</a-button>
                    <a-button size="small" id="tag_show_btn2" type="primary" @click="funTagShow" v-show="tagShow">收起</a-button>
                  </span>
                </div>
              </div>
            </div>
          </a-col>
          <a-col :xl="24" :lg="24" :md="24" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons" >
              <a-button type="primary" @click="searchQuery" icon="search" >查询</a-button >
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px" >重置</a-button >
              <a-button type="primary" @click="updateTagId" icon="deployment-unit" style="margin-left: 8px" :disabled="selectedRowKeys.length == 0">分配标签</a-button >
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->

    <!-- table区域-begin -->
    <div>
      <a-table 
        ref="table" 
        size="middle" 
        bordered 
        rowKey="id" 
        :scroll="{ x: 1300 }" 
        :columns="columns" 
        :dataSource="tableData" 
        :pagination="ipagination" 
        :loading="loading" 
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        @change="handleTableChange" >
        <!--操作-->
        <span slot="action" slot-scope="text, record" >
          <a-button type="primary" @click="handleDetail(record)">详情</a-button>
          <a-popconfirm title="确认禁用?" ok-text="确定" cancel-text="取消" @confirm="handleProhibit(record,1)">
            <a-button type="danger" v-show="record.status == 0">禁用</a-button>
          </a-popconfirm>
          <a-popconfirm title="确认可用?" ok-text="确定" cancel-text="取消" @confirm="handleProhibit(record,0)">
            <a-button type="primary" v-show="record.status == 1">可用</a-button>
          </a-popconfirm>
        </span>
        <!--图片预览块-->
        <span slot="imgPath" slot-scope="text, record">
          <img :src="record.avatar" width="100px" height="100px" />
        </span>
        <!--标签-->
        <span slot="tagList" slot-scope="text, record">
          <a-tag class="tag_li" color="green" v-for="(item,index) in record.tagList" :key="index">{{item}}</a-tag>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <!-- <sale-list-modal ref="updateForm" @closeModal="closeModal"></sale-list-modal> -->
    <!-- 选择标签 -->
    <tag-list-modal ref="tagForm" @closeModal="closeModal"></tag-list-modal>
  </a-card>
</template>

<script>
import moment from 'moment'
// import SaleListModal from './modules/saleListModal'
import tagListModal from './modules/tagListModal'
import ApiTree from "@/api/personnel/member";
import TagList from "@/api/common/tagList";
import { Slime, crud } from "@/mixins/slime";

export default {
  name: "memberList",
  mixins: [Slime],
  components: {
    // SaleListModal,
    tagListModal,
  },
  data() {
    return {
      sexUl:[
        { name: "未知", val: 0 },
        { name: "男", val: 1 },
        { name: "女", val: 2 },
      ],
      typeUl:[
        { name: "普通用户", val: 0 },
        { name: "技师", val: 1 },
        { name: "商家", val: 2 },
        { name: "商家子账户", val: 3 },
      ],
      statusUl: [
        { name: "可用", val: 0 },
        { name: "禁用", val: 1 },
        { name: "注销", val: 2 },
      ],
      //tag
      tagUl:[
        // {name: "", val:1, choose:false}
      ],
      tagUlValue:[],
      tagShow: false,
      tableData: [],
      columns: [
        {
          title: "名字",
          align: "center",
          dataIndex: "nickname",
        },
        {
          title: "头像",
          align: "center",
          dataIndex: "avatar",
          scopedSlots: { customRender: 'imgPath' },
        },
        {
          title: "手机号",
          align: "center",
          dataIndex: "mobile",
        },
        {
          title: "标签",
          align: "center",
          dataIndex: "tagList",
          width: 100,
          scopedSlots: { customRender: 'tagList' },
        },
        {
          title: "角色",
          align: "center",
          dataIndex: "type",
          customRender: function (val) {
            switch (val) {
              case 0:
                return "普通用户";
              case 1:
                return "技师";
              case 2:
                return "商家";
              case 3:
                return "商家子账户";
              default:
                return "未知";
            }
          },
        },
        {
          title: "性别",
          align: "center",
          dataIndex: "sex",
          customRender: function (val) {
            switch (val) {
              case 0:
                return "未知";
              case 1:
                return "男";
              case 2:
                return "女";
              default:
                return "未知";
            }
          },
        },
        {
          title: "地区",
          align: "center",
          dataIndex: "provinceName",
          customRender: function (val,record) {
            if(record.provinceName || record.cityName || record.districtName){
              return record.provinceName + record.cityName + record.districtName
            }else{
               return '未填写'
            }
          },
        },
        {
          title: "状态",
          align: "center",
          dataIndex: "status",
          customRender: function (status) {
            switch (status) {
              case 0:
                return "可用";
              case 1:
                return "禁用";
              case 2:
                return "注销";
              default:
                return "未知";
            }
          },
        },
        {
          title: "时间",
          align: "center",
          dataIndex: "createTime",
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
    };
  },
  created() {
    crud(ApiTree);
    this.loadData();
    this.tagList()
  },
  methods: {
    // 加载表格数据
    loadData(arg) {
      this.loadDataPack(arg).then((res) => {
        let tableData = [];
        if (res.result) {
          tableData = res.result.records;
        }
        this.tableData = tableData;
      });
    },
    //标签
    tagList(){
      let param = {
        type:2
      }
      TagList.getList(param).then((res) => {
        let tagUl = []
        tagUl = res.result.map((obj)=>{
          return obj = {
            val: obj.id, 
            name: obj.name,
            choose: false,
          }
        })
        this.tagUl = tagUl
      })
    },
    //选择标签
    handleTagsChange(index){
      this.tagUl[index].choose = !this.tagUl[index].choose
      let tagIds = []
      for(let i=0;i<this.tagUl.length;i++){
        if(this.tagUl[i].choose){
          tagIds.push(this.tagUl[i].val)
        }
      }
      this.queryParam.tagIds = tagIds.join(',')
      // this.tagUlValue = value
      // let tagIds = value.join(',')
      // this.queryParam.tagIds = tagIds
    },
    // 查 重置
    searchReset() {
      this.queryParam = {};
      this.tagUl = this.tagUl.map((obj)=>{
        return obj = {
          ...obj,
          choose:false,
        }
      })
      this.queryParamRangePicker = null;
      this.$router.push({ query: {} })
      this.loadData(1);
    },
    //选择用户
    onSelectChange(selectedRowKeys){
      this.selectedRowKeys = selectedRowKeys;
    },
    //更新标签-打开
    updateTagId(){
      let tagList = []
      if(this.selectedRowKeys.length == 1){
        let id = this.selectedRowKeys[0]
        ApiTree.getDetail(id).then((res) => {
          let data = res.result;
          tagList = data.tagList ? data.tagList : []
          let memberList = this.selectedRowKeys
          this.$refs.tagForm.open(tagList,memberList)
        });
      }else{
        let memberList = this.selectedRowKeys
        this.$refs.tagForm.open(tagList,memberList)
      }
    },
    funTagShow(){
      this.tagShow = !this.tagShow
    },
    //详情
    handleDetail(record){
      this.$router.push({ path: '/memberDetail?id=' + record.id})
    },
    //选择日期
    panelChange(date){
      let startDate = moment(date[0]).format('YYYY-MM-DD');
      let endDate = moment(date[1]).format('YYYY-MM-DD');
      this.queryParam = {...this.queryParam, startCreateDate:startDate, endCreateDate:endDate}
    },
    //禁止
    handleProhibit(record,val){
      let that = this
      let id = record.id
      ApiTree.getDetail(id).then((res)=>{
        let data = res.result
        data = {...data, status:val }
        ApiTree.getUpdate(data).then((res)=>{
          that.$message.success('禁止成功');
        })
        .finally(() => {
          this.loadData(2);
          this.imgEditShow = false
        });
      })
    },
  },
};
</script>
<style scoped>
</style>